<?php

namespace app\api\controller;
use \app\api\model\Account;
class Base
{

    //注册功能
    public function register()
    {
        //传入学号，姓名，密码注册
        $no=input('get.no');
        $name=input('get.name');
        $password=input('get.password');
        if(empty($no)){
            output_error('学号不能为空~');
        }
        if(empty($name)){
            output_error('姓名不能为空~');
        }
        if(empty($password)){
            output_error('密码不能为空~');
        }
        $data=Account::where('member_no','=',$no)->find();
        if(!empty($data))output_error('学号已存在~');
        $condition=array();
        $condition['member_no']=$no;
        $condition['member_name']=$name;
        $condition['member_password']=md5($password);
        $condition['member_token']='-1';
        $condition['member_gender']='1';
        $condition['add_time']=time();
        Account::insert($condition);
        output_error('1');
        //规则1：学号唯一
        //规则2：密码md5加密
    }

    //找回密码
    public function findpass()
    {
        //通过学号，姓名重置密码
        $no=input('get.no');
        $name=input('get.name');
        $password=input('get.password');
        if(empty($no))output_error('学号不能为空');
        if(empty($name))output_error('姓名不能为空');
        $condition=array();
        $condition[]=['member_no','=',$no];
        $condition[]=['member_name','=',$name];
        $data=Account::where($condition)->find();
        if(empty($data))output_error('学号与姓名不匹配，无法重置密码');
        Account::where('member_id','=',$data['member_id'])->updata(array('member_password'=>md5($password)));
        output_error("1");
        //规则1：密码md5加密
    }

    //登录功能
    public function login()
    {
        //通过学号，密码登录，返回Token
        $no=input('get.no');
        $password=input('get.password');
        if(empty($no))output_error('学号不能为空');
        if(empty($password))output_error('密码不能为空');
        $condition =array();
        $condition[]=['member_no','=',$no];
        $condition[]=['member_password','=',$password];
        $data=Account::where($condition)->find();
        if(empty($data)){
            output_error('学号或密码错误');
        }
        $token=md5($no.time());
        Account::where('member_id','=',$data['member_id'])->updata(array('member_token' => $token));
        output_error($token);
        //规则1：token由学号+当前时间戳md5加密生成
    }

}
